/*
 * Grid.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   Grid.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   16 jan 2013
 */

#include <wx/grid.h>
#include <iostream>

#include "Grid.hpp"


namespace tltgen {

  Grid::Grid(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name) :
    wxGrid(parent, id, pos, size, style | wxWANTS_CHARS, name),
    tabWrap(false)
  {
    this->Connect(wxEVT_KEY_DOWN, wxKeyEventHandler(Grid::OnKeyDown), NULL, this);
  }

  void Grid::SetTabWrap(bool value)
  {
    this->tabWrap = value;
  }
  bool Grid::IsTabWrap() const
  {
    return this->tabWrap;
  }

  void Grid::OnKeyDown(wxKeyEvent& event)
  {
    if (! IsTabWrap() || event.GetKeyCode() != WXK_TAB) {
      event.Skip();
      return;
    }

    bool keyHandled = false;
    if (event.ShiftDown()) {
      if (GetGridCursorCol() == 0 && GetGridCursorRow() > 0) {
	SetGridCursor(GetGridCursorRow() - 1, GetNumberCols() - 1);
	keyHandled = true;
      }
    } else {
      if (GetGridCursorCol() == GetNumberCols() - 1 && GetGridCursorRow() < GetNumberRows() - 1) {
	SetGridCursor(GetGridCursorRow() + 1, 0);
	keyHandled = true;
      }
    }

    if (! keyHandled) {
      event.Skip();
    }
  }
}

